<?php

/**
 * contact : 刘经理 19326416371
 * company : 版权所属 四川北星惯牌科技有限公司
 **/

namespace app\common\manager;

use app\common\model\MoneyLog;
use app\common\model\ScoreLog;
use think\Db;
use think\Exception;

class UserScoreManager extends BaseManager
{
    /**
     * 积分变更类型 1=增加,2=减少
     */
    CONST TYPE_INCREASE = 1;
    CONST TYPE_DECREASE = 2;

    CONST TYPE_TEAM = 1;
    CONST TYPE_INCREASE_TEAM = 11;
    CONST TYPE_DECREASE_TEAM = 12;
    CONST TYPE_MATCH = 2;
    CONST TYPE_INCREASE_MATCH = 21;
    CONST TYPE_DECREASE_MATCH = 22;
    CONST TYPE_INVITE = 3;
    CONST TYPE_INCREASE_INVITE_INVITED = 31;
    CONST TYPE_INCREASE_INVITE_USER = 32;

    CONST TYPE_SYS_CHANGE = 5;
    CONST TYPE_INCREASE_TEAM_MESSAGE = '用户%s 于 %s 组队成功,收入%.2f积分';
    CONST TYPE_DECREASE_TEAM_MESSAGE = '用户%s 于 %s 组队成功,支出%.2f积分';
    CONST TYPE_INCREASE_MATCH_MESSAGE = '用户%s 于 %s 参加比赛,收入%.2f积分';
    CONST TYPE_DECREASE_MATCH_MESSAGE = '用户%s 于 %s 参加比赛,支出%.2f积分';
    CONST TYPE_INCREASE_INVITE_INVITED_MESSAGE = '用户%s 于 %s 成功邀请用户注册平台,奖励%.2f积分';
    CONST TYPE_INCREASE_INVITE_USER_MESSAGE = '用户%s 于 %s 受用户邀请成功注册平台,奖励%.2f积分';
    CONST TYPE_SYS_CHANGE_MESSAGE = '用户%s 于 %s 系统变更%.2f积分';
    CONST MEMO_COLLECTION = [
        self::TYPE_INCREASE_TEAM => [
            'value'=>self::TYPE_INCREASE_TEAM,
            'message'=>self::TYPE_INCREASE_TEAM_MESSAGE,
        ],

        self::TYPE_DECREASE_TEAM => [
            'value'=>self::TYPE_DECREASE_TEAM,
            'message'=>self::TYPE_DECREASE_TEAM_MESSAGE,
        ],
        self::TYPE_INCREASE_MATCH => [
            'value'=>self::TYPE_INCREASE_MATCH,
            'message'=>self::TYPE_INCREASE_MATCH_MESSAGE,
        ],
        self::TYPE_DECREASE_MATCH => [
            'value'=>self::TYPE_DECREASE_MATCH,
            'message'=>self::TYPE_DECREASE_MATCH_MESSAGE,
        ],
        self::TYPE_INCREASE_INVITE_INVITED => [
            'value'=>self::TYPE_INCREASE_INVITE_INVITED,
            'message'=>self::TYPE_INCREASE_INVITE_INVITED_MESSAGE,
        ],
        self::TYPE_INCREASE_INVITE_USER => [
            'value'=>self::TYPE_INCREASE_INVITE_USER,
            'message'=>self::TYPE_INCREASE_INVITE_USER_MESSAGE,
        ],
        self::TYPE_INCREASE_INVITE_USER => [
            'value'=>self::TYPE_INCREASE_INVITE_USER,
            'message'=>self::TYPE_INCREASE_INVITE_USER_MESSAGE,
        ]
    ];

    /**
     * 积分日志
     * @param object $userAccount
     * @param int $userId
     * @param float $score
     * @param int $detail
     * @param int $outIn
     * @param string $message
     * @param array $extend
     * @param bool $trans
     * @return array
     */
    public static function UserScoreLog(object $userAccount,int $userId, float $score,int $detail,int $outIn,string $message,array $extend=[],bool $trans=true)
    {
        if ($trans){
            Db::startTrans();
        }
        try {
            ScoreLog::create([
                'user_id' => $userId,
                'type' => $outIn,
                'detail' => $detail,
                'score' => $score,
                'before' => $userAccount->score,
                'after' => bcadd($userAccount->score,$score,2),
                'memo' => $message,
                'relation_id' => isset($extend['relation_id']) ?? 0,
                'relation_order' => isset($extend['relation_order']) ?? '',
                'createtime' => time(),
            ]);

            if ($trans){
                Db::commit();
            }
            self::setStatusCode(self::$successCode);
            self::setMessage(self::$successMessage);
        }catch (Exception $exception){
            if ($trans){
                Db::rollback();
            }
            self::setStatusCode(self::$failCode);
            self::setMessage($exception->getMessage());
        }

        return ['code'=>self::getStatusCode(),'message'=>self::getMessage()];
    }
}